const RoleObj = require('../modules/Rules');
const AuthorityObj = require('../modules/Authority');
const Administor = require('../modules/Administor');
const CourseObj = require('../modules/Course');

let BackstageController = {
    //显示后台登录界面
    get_login: function (req, res) {
        res.type('html');
        this.fetch(req, res, 'login');
    },

    // 验证后台登录信息
    post_login: function (req, res) {
        let data = req.body;
        // console.log(data);
        req.session.aname = req.body.username;
        // console.log(req.session.aname);
        let resultDate = Administor.checkLogin(data);
        resultDate.then(
            result => {
                res.send(result);
            }
        )
    },

    // 退出登录
    get_logout: function (req, res) {
        req.session.destroy()
        res.send({
            code: 200,
            msg: 'logout success',
            url: '/backstage/login'
        })
    },

    //显示后台主页
    get_index: async function (req, res) {
        res.type('html');
        // console.log(sessionStorage.getItem('userName'), 111);
        let aname = req.session.aname;
        // console.log(aname);
        let roleArr = [];
        await RoleObj.show_rules(aname)
            .then(
                result => {
                    roleArr = result;
                }
            )
        this.fetch(req, res, '', {
            roleArr,
            aname
        })
    },

    //显示教师管理
    get_teacher: async function (req, res) {
        let aname = req.session.aname;
        let roleArr = [];
        await RoleObj.show_rules(aname)
            .then(
                result => {
                    roleArr = result;
                }
            )
        res.type('html');
        this.fetch(req, res, 'teacher', {
            roleArr,
            aname
        })
    },
    //显示学生管理
    get_student: async function (req, res) {
        res.type('html');
        let aname = req.session.aname;
        let roleArr = [];
        await RoleObj.show_rules(aname)
            .then(
                result => {
                    roleArr = result;
                }
            )
        this.fetch(req, res, 'student', {
            roleArr,
            aname
        })
    },

    //显示课程管理
    get_course: async function (req, res) {
        res.type('html');
        let aname = req.session.aname;
        let roleArr = [];
        let courseArr = [];
        await RoleObj.show_rules(aname)
            .then(
                result => {
                    roleArr = result;
                }
            )

        //显示所有的课程
        await CourseObj.show_courses()
                .then(
                    result => {
                        courseArr = result;
                        // console.log(courseArr);
                    }
                )
                
        this.fetch(req, res, 'course', {
            roleArr,
            aname,
            courseArr
        })
    },

    //显示角色管理
    get_role: async function (req, res) {
        res.type('html');
        let aname = req.session.aname;
        let roleArr = [];
        let adminArr = [];

        //显示所有的管理
        await RoleObj.show_rules(aname)
            .then(
                result => {
                    roleArr = result;
                }
            )

        //显示所有管理员
        await RoleObj.show_allRole()
            .then(
                result => {
                    adminArr = result;
                }
            )
                // console.log(adminArr);
        this.fetch(req, res, 'role', {
            roleArr,
            adminArr,
            aname
        })
    },

    //显示权限管理
    get_authority: async function (req, res) {
        res.type('html');
        let aname = req.session.aname;
        let roleArr = [];
        let authorityArr = [];
        //显示所有的管理
        await RoleObj.show_rules(aname)
            .then(
                result => {
                    roleArr = result;
                }
            )

        //显示所有的权限
        await AuthorityObj.show_allAuthority(aname)
            .then(
                result => {
                    authorityArr = result;
                }
            )
        // console.log(authorityArr);
        this.fetch(req, res, 'authority', {
            roleArr,
            authorityArr,
            aname
        })
    },

    //显示所有权限
    get_allAuthority: async function (req, res) {
        res.type('html');
        let aname = req.session.aname;
        this.fetch(req, res, 'authority', {
            authorityArr,
            aname
        })
    },

    //显示角色管理页面的所有权限
    get_roleAuthority: async function(req, res) {
        res.type('html');
        let aname = req.session.aname;
        let roleArr = [];

        //显示所有的管理
        await RoleObj.show_rules(aname)
            .then(
                result => {
                    roleArr = result;
                }
            )
        this.fetch(req, res, 'roleAuthority', {
            roleArr,
            aname
        })
    }
}

BackstageController.__proto__ = require('../../common/BaseController')

module.exports = BackstageController